package com.pz.cloud.framework.business.workflow.model;


import com.baomidou.mybatisplus.extension.service.IService;
import com.pz.cloud.framework.base.model.WorkflowEntity;
import com.pz.cloud.framework.business.workflow.entity.ProcessInfo;
import com.pz.cloud.framework.business.workflow.enums.WorkflowEnum;
import com.pz.cloud.framework.business.workflow.service.IProcessInfoService;
import com.pz.cloud.framework.business.workflow.service.IWorkflowService;
import com.pz.cloud.framework.util.BizUtil;
import com.pz.cloud.framework.util.WorkflowUtil;
import org.jeecg.common.util.BeanConvertUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * activiti任务完成后的回调接口定义
 */
public abstract class BaseTaskCallback<T extends WorkflowEntity,S extends IService<T>> {
    @Autowired
    protected S service;

    public abstract void callback(T obj);

    protected ProcessInfo processInfo = null;
    /**
     * activiti任务完成后的回调
     */
    public void execute(T obj) {
        String id = BizUtil.getIdFieldValue(obj);
        T entity = service.getById(id);
        if(entity!=null){
            Map<String, Object> field = obj.getWorkflowModel().getTaskField();

            BeanConvertUtil.convert(field,entity);

            IProcessInfoService processInfoService = SpringContextUtils.getBean(IProcessInfoService.class);
            processInfo = processInfoService.getByRealtedId(id);
            if(processInfo!=null){
                entity.setActStatus(processInfo.getActStatus());
                service.updateById(entity);
            }
            callback(entity);
        }
    }
    /**
     * activiti任务完成后的回调
     */
    public void execute(ProcessInfo processInfo) {
        this.processInfo = processInfo;
        T entity = service.getById(processInfo.getRelatedId());
        if(entity!=null){
            entity.setActStatus(processInfo.getActStatus());
            service.updateById(entity);
            callback(entity);
        }
    }
    /**
     * 流程撤回
     */
    public void revoke(ProcessInfo processInfo) {
        T entity = service.getById(processInfo.getRelatedId());
        if(entity!=null){
            entity.setActStatus(processInfo.getActStatus());
            service.updateById(entity);
        }
    }
}
